package com.mdd.front.controller.order;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.RechargeOrder;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.order.IOrderService;
import com.mdd.front.service.order.IRechargeOrderService;
import com.mdd.front.service.system.ISystemDevPayWayService;
import com.mdd.front.validate.OrderParam;
import com.mdd.front.validate.PageParam;
import com.mdd.front.validate.RechargeOrderParam;
import com.mdd.front.vo.order.OrderDetailVo;
import com.mdd.front.vo.order.OrderListVo;
import com.mdd.front.vo.order.RechargeOrderListVo;
import com.mdd.front.vo.system.SystemDevPayWayDetailVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 充值订单管理
 * @author panweiliang
 */
@RestController
@RequestMapping("/api/recharge0rder")
public class RechargeOrderController {

    @Resource
    private IRechargeOrderService iRechargeOrderService;

    /**
     * 充值订单列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        PageResult<RechargeOrderListVo> list = iRechargeOrderService.list(pageParam, userId);
        return AjaxResult.success(list);
    }


    /**
     * 订单新增
     *
     * @author panweiliang
     * @param rechargeOrderParam 参数
     * @return Object
     */
    @PostMapping("/add")
    public Object add(@Validated(value = RechargeOrderParam.create.class) @RequestBody RechargeOrderParam rechargeOrderParam) throws WxPayException {
        Integer userId = LikeFrontThreadLocal.getUserId();
        rechargeOrderParam.setUserId(userId);
        return AjaxResult.success(iRechargeOrderService.add(rechargeOrderParam));
    }

    /**
     * 订单完成支付
     *
     * @author panweiliang
     * @param rechargeOrderParam 参数
     * @return Object
     */
    @PostMapping("/paystatus/completed")
    public Object updateOrderPayStatusToPaid(@Validated(value = RechargeOrderParam.change.class) @RequestBody RechargeOrderParam rechargeOrderParam) throws WxPayException {
        return AjaxResult.success(this.iRechargeOrderService.updateOrderPayStatusToPaid(rechargeOrderParam.getId()));
    }

    @GetMapping("/detail")
    public Object getRechargeOrder(@RequestParam("id") Integer id) {
        return AjaxResult.success(this.iRechargeOrderService.getDetailById(id));
    }

}
